﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SpringBeansEditor.Providers;
using System.Reflection;

namespace SpringBeansEditor.GUIHelpers
{
    /// <summary>
    /// Interaction logic for AssemblyLoader.xaml
    /// </summary>
    public partial class AssemblyLoader : UserControl
    {
        public AssemblyLoader()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Opens a file dialog to allow the user to sleect multiple assembly files.
        /// Once the dialog is closed, the assemblies field will be populated and loaded
        /// for reflection.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BrowseBtn_Click(object sender, RoutedEventArgs e)
        {

            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = ".Net Assemblies";
            dlg.Filter = ".NET Assemblies (.exe, .dll)|*.exe;*.dll"; // Filter files by extension
            dlg.Multiselect = true;

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                string[] paths = dlg.FileNames;
                foreach (var path in paths)
                {
                    AssembliesList.Items.Add(path);
                }
            }
        }

        private void LoadAssemblies()
        {
            var reflection_provider = TypesProvider.GetInstance();
            List<string> assemblies = new List<string>();
            foreach (var item in AssembliesList.Items)
            {
                assemblies.Add(item.ToString());
            }
            try
            {

                reflection_provider.LoadAssemblies(assemblies.ToArray());
            }
            catch (ReflectionTypeLoadException e)
            {
                foreach (var load_exception in e.LoaderExceptions)
                {
                    MessageBox.Show(e.Message, "Error loading assemblies");
                }
            }
        }

        private void AddBtn_Click(object sender, RoutedEventArgs e)
        {
            AssembliesList.Items.Add(AssembliesToLoadTxt.Text);
        }

        private void RemoveBtn_Click(object sender, RoutedEventArgs e)
        {
            if (AssembliesList.SelectedItem != null)
            {
                AssembliesList.Items.Remove(AssembliesList.SelectedItem);
            }
        }

        private void LoadBtn_Click(object sender, RoutedEventArgs e)
        {
            LoadAssemblies();
        }


    }
}
